import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class MyUserBasedRecommender {
	
	public List<RecommendedItem> myUserBasedRecommender(long userId, int size) {
		List<RecommendedItem> recommendedItems = null;
		try {
			DataModel model = new FileDataModel(new File("data/ratingsForMahout.dat"));
			UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
			UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.3, similarity, model);
			Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
			recommendedItems = recommender.recommend(userId, size);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return recommendedItems;
	}
	
	public static void main(String args[]) throws IOException, Exception {
		MyUserBasedRecommender recommender = null;
		List<RecommendedItem> recommendedItems = null;
		
		recommender = new MyUserBasedRecommender();
		recommendedItems = recommender.myUserBasedRecommender(3, 20);
		for (RecommendedItem item : recommendedItems) {
			System.out.println(item);
		}
	}
}
